<?
ini_set( "max_execution_time", 3000000 );
include_once "../classes/init.php";

$fini = false;
while( !$fini ) {
	/* vas chercher tous les code postaux que la BD possède */
	$req = "SELECT codePostal FROM codePostaux WHERE latitude = '46.852397' LIMIT 1000 ";
	$tabCP = $objBD->query( $req );
	if( count( $tabCP ) == 0 ) {
		$fini = true;
	}
	foreach( $tabCP as $cp1 ) {
		$tab = array();	
		$cp = split( ' ', $cp1[ 'codePostal' ] );
		$page = fopen("http://www.postalcodedownload.com/" . $cp[ 0 ] . "/" . $cp[ 1 ]  . "/index.html", "rb");
		if( !empty( $page ) ) {
			$contenu = stream_get_contents( $page );
			@$doc = DOMDocument::loadHTML( $contenu );
			$objXpath = new domxpath( $doc );
			$req = "/html/body/table/tr/td/font/table/tr/td/table/tr/td";
			$objNodeList = $objXpath->query( $req );
			foreach ( $objNodeList as $node ){
				$tab[] = str_replace( "\t", "", str_replace( "\n", "", strip_tags( trim( $node->nodeValue ) ) ) );
			}
		}
		if( !empty( $tab ) ) {
			$req = "UPDATE codePostaux SET latitude = '" . $tab[ 17 ] . "', longitude = '" . $tab[ 19 ] . "', timezone = " . $tab[ 13 ] . ", ville = '" . addslashes( $tab [ 3 ] ) . "', province = '" . $tab[ 5 ] . "' WHERE codePostal = '" . $cp1[ 'codePostal' ] . "'";
			$objBD->query( $req );
		}
	}
}
?>